<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.value.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.value.class.php');

class FormDataBase extends FormValueBase {
	
	//constructor
	function FormDataBase() {
		parent::FormValueBase();
		
		$this->fObj = new FormBase();
		$this->fvObj = new FormValueBase();
		$this->fiObj = new FormItemBase();
		$this->fivObj = new FormItemValueBase();
	}
	
	//methods
	function addFormData($fid, $data_array) {
		if( $fid == 0 || !is_array($data_array) ) {
			return false;	
		}
		//form
		$d = array('_last_action' => 'time()');
		$this->fObj->updateItem($fid, $d);
		//form values
		$d = array('_fid' => $fid);
		$vid = $this->addItem($d);
		
		//form items values
		$ds = $this->_post_field($data_array, '', 'field_fi', true);
		if( !is_array($ds) ) {
			return false;
		}
		foreach($ds as $k => $v) {
			if( is_array($v) ) {
				$v = implode(',', $v);
			}
			$d = array('_vid' => $vid, '_iid' => $k, '_value' => $v);
			$this->fivObj->addItem($d);
		}
		
		return $vid;
	}
	
	//
	function getFormValue($vid) {
		$value = $this->getItem($vid);
		$values = $this->fivObj->getItemsDetail(array('vid' => $vid), -1, false);
		
		return array('value' => $value, 'values' => $values);
	}
	
	function getFormValues($fid, $filter = '', $check = 0, $limit = true) {
		if( !is_array($filter) ) {
			$filter = array();
		}
		$filter = array('fid' => $fid);
		$ids = $this->getItemsIDs($filter, $check, $limit);
		for($i=0, $n=sizeof($ids); $i<$n; $i++) {
			$values[] = $this->getFormValue($ids[$i]);
		}
		
		return $values;
	}
}
//
?>